<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="goodsParamsInfo.query"
            class="input-with-select"
            clearable
            @clear="getGoodsList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList()"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品区域 -->
        <el-col :span="4">
          <el-button type="primary" @click="skipAddGoods()">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <el-table :data="goodsListData" stripe style="width: 100%" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70px">
        </el-table-column>
        <el-table-column label="创建时间" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | format('yyyy-MM-dd hh:mm:ss') }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="goodsListEditBtn(scope.row)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoods(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="goodsListHandleSizeChange"
        @current-change="goodsListHandleCurrentChange"
        :current-page="goodsParamsInfo.pagenum"
        :page-sizes="[10, 30, 50, 100]"
        :page-size="goodsParamsInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodsListTotal"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取商品列表参数
      goodsParamsInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数据
      goodsListData: [],
      // 商品列表总数
      goodsListTotal: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.goodsParamsInfo
      })
      // 总数
      this.goodsListTotal = res.data.total
      // 数据
      this.goodsListData = res.data.goods
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //   提示
      this.$message.success(res.meta.msg)
    },
    // pageSize 改变时会触发
    goodsListHandleSizeChange(size) {
      this.goodsParamsInfo.pagesize = size
      // 重新发送请求刷新页面
      this.getGoodsList()
    },
    // currentPage 改变时会触发
    goodsListHandleCurrentChange(page) {
      this.goodsParamsInfo.pagenum = page
      // 重新发送请求刷新页面
      this.getGoodsList()
    },
    // 删除商品
    async removeGoods(scope) {
      console.log(scope)
      // 提示信息
      const resConfirm = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(error => error)
      if (resConfirm !== 'confirm') return this.$message.info('已取消删除')
      // 发送请求
      const { data: res } = await this.$http.delete(`goods/${scope.goods_id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 提示
      this.$message.success(res.meta.msg)
      // 重新获取数据
      this.getGoodsList()
    },
    // 编辑商品
    async goodsListEditBtn(scope) {
      const { data: res } = await this.$http.get(`goods/${scope.goods_id}`)
      console.log(res)
    },
    // 跳转到添加商品页面
    skipAddGoods() {
      this.$router.push('goods/add')
    }
  }
}
</script>

<style lang="less" scoped></style>
